package com.yztc.administrator.meishi.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.yztc.administrator.meishi.R;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CollectionActivity extends AppCompatActivity {

    private String next1;
    private List<String> listCaption;//保存详情的数组
    private List<String> listUrl;//保存url的数组
    private ListView mLv;
    private SharedPreferences video;//sp存储的数据
    private ArrayAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collection);
        TextView tv_empty = (TextView) findViewById(R.id.tv_empty);
        mLv = (ListView) findViewById(R.id.lv_coll);
        //得到本地video存储的数据
        video = getSharedPreferences("video", MODE_PRIVATE);
        //获取本地存储数据的集合
        Map<String, Set<String>> all = (Map<String, Set<String>>) video.getAll();
        Collection<Set<String>> values = all.values();
        Iterator<Set<String>> iterator = values.iterator();
        listCaption = new ArrayList<>();
        listUrl = new ArrayList<>();
        //进行迭代遍历数据
        while (iterator.hasNext()) {
            Set<String> next = iterator.next();
            Iterator<String> iterator1 = next.iterator();
            while (iterator1.hasNext()) {
                next1 = iterator1.next();
                if (isContainsChinese(next1)) {
                    listCaption.add(next1);
                } else {
                    listUrl.add(next1);
                }
            }
        }
        //创建适配器
        adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, listCaption);
        mLv.setAdapter(adapter);
        //填充布局
        mLv.setEmptyView(tv_empty);
        //item点击事件
        onItemClick();
    }

    /* *
       * @author: ZTC
       * @Description: item点击事件
       * @data:  2016/9/23  9:41
       * @version: V1.0
       */
    private void onItemClick() {
        //短按事件
        mLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //创建意图跳转到详情播放界面
                Intent intent = new Intent(CollectionActivity.this,WebViewActivity.class);
                String url = listUrl.get(position);
                String caption = listCaption.get(position);
                intent.putExtra("path",url);
                intent.putExtra("caption",caption);
                //开启意图
                startActivity(intent);
            }
        });
        //长按事件
        mLv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                //删除本地数据
                SharedPreferences.Editor edit = video.edit();
                String caption  = (String) mLv.getItemAtPosition(position);
                edit.remove(caption);
                //如果提交成功tosat删除成功
                if (edit.commit()){
                    Toast.makeText(CollectionActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                }
                //删除list数据
                listCaption.remove(position);
                //刷新适配器
                adapter.notifyDataSetChanged();
                return true;

            }
        });
    }

    /**
     * 判断字符串是否包含中文
     *
     * @param str
     * @return
     */
    public static boolean isContainsChinese(String str) {
        String regEx = "[\u4e00-\u9fa5]";
        Pattern pat = Pattern.compile(regEx);
        Matcher matcher = pat.matcher(str);
        boolean flg = false;
        if (matcher.find()) {
            flg = true;
        }
        return flg;
    }


}
